package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBill;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillParam;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author jiangge
 * @Date 2020-09-15 20:24:02
 */
@Controller
@RequestMapping("/dsstoreBill")
public class DsstoreBillController extends BaseController {

    private String PREFIX = "/modular/system/dsstoreBill";

    @Autowired
    private DsstoreBillService dsstoreBillService;

    /**
     * 跳转到主页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/dsstoreBill.html";
    }

    /**
     * 新增页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/dsstoreBill_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/dsstoreBill_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.add(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.update(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DsstoreBillParam dsstoreBillParam) {
        this.dsstoreBillService.delete(dsstoreBillParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DsstoreBillParam dsstoreBillParam) {
        DsstoreBill detail = this.dsstoreBillService.getById(dsstoreBillParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jiangge
     * @Date 2020-09-15
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DsstoreBillParam dsstoreBillParam) {
        return this.dsstoreBillService.findPageBySpec(dsstoreBillParam);
    }


    /**
     * @param timeLimit 时间范围
     * @param dm        店名
     * @param pl        品类
     * @return
     */
    @RequestMapping(value = "/getStoreData")
    @ResponseBody
    public Object list(String timeLimit, String dm, String pl) {
        String startTime;
        String endTime;
        //日期不为空
        if (StringUtils.isNotBlank(timeLimit)) {
            //当月第一天
            startTime = timeLimit.substring(0, 8) + "01";
            endTime = timeLimit.substring(13, 23);
        } else {
            SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
            String time = sformat.format(new Date());
            StringBuilder sb = new StringBuilder(time);
            sb.replace(time.length() - 2, time.length(), "01");
            startTime = sb.toString();
            endTime = time;

        }
        //查询
        List<Map<String, Object>> dataList = Lists.newLinkedList();
        Map<String, Object> dataMap = null;
        DsstoreBillParam param = new DsstoreBillParam();
        param.setShopCode(dm);
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        param.setItemTypeCode("101");
        List<DsstoreBill> list = dsstoreBillService.getConutDataResult(param);
        for (int i = 0; i < list.size(); i++) {
            dataMap = Maps.newLinkedHashMap();
            dataMap.put("shopName", list.get(i).getShopName());
            //品类
            dataMap.put("pl", list.get(i).getItemSmallClassName());
            //项名
            dataMap.put("xm", list.get(i).getItemName());
            //出库量或者入库量
            dataMap.put("result", list.get(i).getItemBalanceBusAmout().toString());
            //单位
            dataMap.put("unit", list.get(i).getItemBusUnit());
            //日期
            dataMap.put("date", timeLimit);
            //预警金额
            dataMap.put("printAmount", list.get(i).getWarnPrice());
            dataList.add(dataMap);
        }
        Page<Map<String, Object>> page = new Page<Map<String, Object>>(1, dataList.size(), dataList.size());
        page.setRecords(dataList);
        return LayuiPageFactory.createPageInfo(page);
    }


    /**
     * 处理不合理数据，0E-8，0e-8
     */
    private String convertNum(String data) {
        if ("0E-8".equals(data) || "0-8".equals(data) || "0E-12".equals(data) || "0e-12".equals(data)) {
            return "0";
        } else {
            return data;
        }
    }
}


